package com.bblei.hbaseDemo.mapReduceDemo;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;

import static com.bblei.hbaseDemo.mapReduceDemo.HBaseMr.col;
import static com.bblei.hbaseDemo.mapReduceDemo.HBaseMr.colf;

/**
 * MyMapper 继承 TableMapper
 * TableMapper<Text,IntWritable>
 * Text:输出的key类型，
 * IntWritable：输出的value类型
 */

public class MyMapper extends TableMapper<Text, IntWritable> {

    private static IntWritable one = new IntWritable(1);
    private static Text word = new Text();

    //输入的类型为：key：rowKey； value：一行数据的结果集Result
    protected void map(ImmutableBytesWritable key, Result value,
                       Context context) throws IOException, InterruptedException {
        //获取一行数据中的colf：col
        String words = Bytes.toString(value.getValue(Bytes.toBytes(colf), Bytes.toBytes(col)));// 表里面只有一个列族，所以我就直接获取每一行的值
        //按空格分割
        String itr[] = words.toString().split(" ");
        //循环输出word和1
        for (int i = 0; i < itr.length; i++) {
            word.set(itr[i]);
            context.write(word, one);
        }
    }

}
